]> git.r.bdr.sh - rbdr/super-polarity/blame - Super Polarity/Actors/StandardShip.cs
Merge branch 'master' of github.com:benbeltran/super-polarity
[rbdr/super-polarity] / Super Polarity / Actors / StandardShip.cs
CommitLineData
2af83e98
BB
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using Microsoft.Xna.Framework;
6using Microsoft.Xna.Framework.Graphics;
7using Microsoft.Xna.Framework.Content;
8using System.Security.Cryptography;
9
10namespace SuperPolarity
11{
12 class StandardShip : Ship
13 {
14
097781e6 15 protected bool Flashing;
2af83e98
BB
16 protected int ChangeRate;
17 protected int CurrentTime;
097781e6 18 public int AngleChangeProbability;
2af83e98
BB
19 protected int BouncePadding;
20 protected float RotationFactor;
21 protected Random Random;
22 protected bool AddingAngle;
23
74c15570 24 public StandardShip(SuperPolarity newGame) : base(newGame) {}
2af83e98
BB
25
26 public override void Initialize(Texture2D texture, Vector2 position)
27 {
28 base.Initialize(texture, position);
29
30 var cryptoResult = new byte[4];
31 new RNGCryptoServiceProvider().GetBytes(cryptoResult);
32
33 ChangeRate = 50;
34 AngleChangeProbability = 50;
35 BouncePadding = 0;
097781e6 36 MaxVelocity = 2;
2af83e98
BB
37 CurrentTime = 0;
38 RotationFactor = (float) (3 * Math.PI / 180);
39 Random = new Random(BitConverter.ToInt32(cryptoResult, 0));
40 AddingAngle = true;
41 }
42
43 public override void Magnetize(Ship ship, float distance, float angle)
44 {
45 if (ship.GetType() == typeof(MainShip)) {
46 base.Magnetize(ship, distance, angle);
47 }
48 }
49
50 public override void Update(GameTime gameTime)
51 {
52 CurrentTime += gameTime.ElapsedGameTime.Milliseconds;
53 if (!Magnetizing)
54 {
55 AutoMove();
56 BounceBack();
57 }
58 ChangeAngle();
59 Position += Velocity;
74c15570 60 UpdateBox();
2af83e98 61 Magnetizing = false;
097781e6
BB
62
63 if (Flashing)
64 {
65 Color = new Color(255, 255, 255, 128);
66 Flashing = false;
67 }
68 else
69 {
70 Color = Color.White;
71 }
2af83e98
BB
72 }
73
74 protected void AutoMove()
75 {
76 float newAngle = Angle;
77
78 if (CurrentTime < ChangeRate)
79 {
80 return;
81 }
82
83 if (Random.Next(AngleChangeProbability) == 2)
84 {
85 AddingAngle = !AddingAngle;
86 }
87
88 CurrentTime = 0;
89
90 if (AddingAngle)
91 {
92 newAngle += (float) ( Random.NextDouble() * RotationFactor);
93 }
94 else
95 {
96 newAngle -= (float) (Random.NextDouble() * RotationFactor);
97 }
98
99 Velocity.X = (float) (MaxVelocity * Math.Cos(newAngle));
100 Velocity.Y = (float) (MaxVelocity * Math.Sin(newAngle));
101 }
102
103 protected void BounceBack()
104 {
105 if (Position.X + Width < -BouncePadding && Velocity.X < 0)
106 {
107 Velocity.X = -Velocity.X;
108 }
109
110 if (Position.Y + Height < -BouncePadding && Velocity.Y < 0)
111 {
112 Velocity.Y = -Velocity.Y;
113 }
114
115 if (Position.X > game.GraphicsDevice.Viewport.Width + BouncePadding && Velocity.X > 0)
116 {
117 Velocity.X = -Velocity.X;
118 }
119
120 if (Position.Y > game.GraphicsDevice.Viewport.Height + BouncePadding && Velocity.Y > 0)
121 {
122 Velocity.Y = -Velocity.Y;
123 }
124 }
74c15570
BB
125
126 public override void Collide(Actor other, Rectangle collision)
127 {
128 if (Dying)
129 {
130 return;
131 }
132
133 if (other.GetType() == typeof(MainShip))
134 {
135 Die();
136 return;
137 }
138
139 if (other.GetType() == typeof(Bullet))
140 {
141 var theBullet = (Bullet)other;
142 TakeDamage(theBullet.Power);
097781e6 143 Flashing = true;
74c15570
BB
144 }
145 }
146
147 protected override void Die()
148 {
149 ActorManager.CheckOut(this);
150 Renderer.CheckOut(this);
151 game.Player.AddScore(Value);
152 game.Player.AddMultiplier(1);
153 }
2af83e98
BB
154 }
155}